package com.tarena.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.tarena.domain.Dept;
import com.tarena.util.HbnUtil;

/**
 * 
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-2-8
 */

public class DeptDao {

	/**
	 * @InsertDept
	 * @infomation Insert a department.
	 * @param name
	 */
	public void insertDept(Dept dept) {
		Session s = HbnUtil.getSession();
		s.beginTransaction();
		s.save(dept);
		s.getTransaction().commit();
		s.close();
	}

	/**
	 * @class Get a department by its deptId.
	 * @param deptId
	 * @return
	 */
	public Dept getDeptById(Integer deptId) {
		Session s = HbnUtil.getSession();
		Dept dept = (Dept) s.get(Dept.class, deptId);
		s.close();
		return dept;
	}

	/**
	 * @class delete a department by the deptId.
	 * @param deptId
	 * @return
	 */
	public void deleteDeptById(Integer deptId) {
		Session s = HbnUtil.getSession();
		s.beginTransaction();
		s.delete(s.get(Dept.class, deptId));
		s.getTransaction().commit();
		s.close();
	}

	/**
	 * @ Get all department .
	 * 
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<Dept> getAllDept() {
		Session s = HbnUtil.getSession();
		Query query = s.createQuery("from Dept");
		List<Dept> deptList = query.list();
		s.close();
		return deptList;
	}

}
